package cn.lwj.webService.service.impl;

import cn.lwj.webService.base.CreateNewKey;
import cn.lwj.webService.entity.JsonResult;
import cn.lwj.webService.entity.TXfCljg;
import cn.lwj.webService.service.DemoService;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service("demoService")
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public class DemoServiceImpl implements DemoService {

    private static Log log = LogFactory.getLog(DemoServiceImpl.class);
    @Resource
    private CreateNewKey createKey;
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public JsonResult getFymc(String fydm) {
        if("".equals(fydm)) return null;
        String fymc = "";
        String sql = "SELECT DMMS fymc FROM `bm_fy` WHERE dm='" + fydm + "'";
        try {
            fymc = jdbcTemplate.queryForObject(sql, String.class);
            return JsonResult.responseData(JsonResult.SUCCESS, "", fymc);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.responseData(JsonResult.ERROR, "", e.getMessage());
        }
    }


    public static void main(String[] args) {
        TXfCljg oldcljg = new TXfCljg();
        oldcljg.setcXfbs("1");
        oldcljg.setcHcyj("测试复制");
        oldcljg.setcCljg("1");
        oldcljg.setcCljgmc("测试对象复制");
        oldcljg.setcCljgsm("");
        oldcljg.setDtTimestamp(new Date());
        oldcljg.setcClrfybm("M00");
        oldcljg.setcClrfymc("重庆高院");
        oldcljg.setcClrbmbm("069");
        oldcljg.setcClrbmmc("立案庭");
        oldcljg.setcClrbm("110");
        oldcljg.setcClrmc("阿丹");
        TXfCljg newCljg = new TXfCljg("", "修改意见", "哈哈哈", "撤回", "试试看", new Date());
        try {
            TXfCljg obj = (TXfCljg) objectClone(oldcljg, newCljg, new String[]{"cxfbs","cClrfybm","cClrfymc","cClrbmbm","cClrbmmc"});
            System.out.println("hahahh");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 把新对象属性完全复制给旧对象
     * @param newObject
     * @param oldObject
     * @return
     */
    public static Object objectClone(Object newObject,Object oldObject) {
        Field[] oldFields = oldObject.getClass().getDeclaredFields();
        Field newField;
        try {
            for (Field oldField : oldFields){
                oldField.setAccessible(true);
                newField = newObject.getClass().getDeclaredField(oldField.getName());
                newField.setAccessible(true);
                newField.set(newObject,oldField.get(oldObject));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return newObject;
    }

    /**
     * 把新对象属性排除掉excludsArray字段赋值给旧对象
     * @param toObject 需要赋值的新对象
     * @param fromObject 被复制的对象
     * @param excludsArray 排除字段  处理后可以不区分大小写
     * @return
     */
    public static Object objectClone(Object toObject, Object fromObject, String[] excludsArray) {
        Field[] oldFields = fromObject.getClass().getDeclaredFields();
        Field newField;
        List<String> excludsList = new ArrayList<>();
        if (excludsArray != null && excludsArray.length > 0 ) {
            //excludsList = Arrays.asList(excludsArray);
            for (String s : excludsArray) {
                excludsList.add(s.toUpperCase());
            }
        }
        try {
            for (Field oldField : oldFields) {
                //if (excludsList.contains(oldField.getName())) {
                if (excludsList.contains(oldField.getName().toUpperCase())) {
                    continue;
                }
                oldField.setAccessible(true);
                newField = toObject.getClass().getDeclaredField(oldField.getName());
                newField.setAccessible(true);
                newField.set(toObject, oldField.get(fromObject));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toObject;
    }
}
